import { bind, nextAnimateFrame, track } from "xbsj-base"
import { DoublePointsAppendEditing } from "../base"
import { ESCesiumViewer } from "@czmSrc/ESCesiumViewer"
import { equalsN3, ESJEditingMode, ESSceneObject } from "earthsdk3"
import { bindNorthRotation, getSceneScaleForScreenPixelSize, positionToCartesian } from "@czmSrc/utils";
import { BaseEditingWrapper } from "./BaseEditingWrapper";
import * as Cesium from 'cesium';
export class DoublePointsAppendEditingWrapper extends BaseEditingWrapper {
    static readonly type = this.registerEditing("ESCesiumViewer", ESJEditingMode.DoublePointsAppend, this);
    constructor(sceneObject: ESSceneObject, czmViewer: ESCesiumViewer) {
        super(sceneObject, czmViewer, true);
        const doublePointsAppendEditing = this.currentEditing = this.ad(new DoublePointsAppendEditing(czmViewer));
        this.ad(doublePointsAppendEditing.enabledChanged.don((newVal) => {
            !newVal && this.ad(nextAnimateFrame(() => {
                this.destroy();
            }));
        }));
        do {
            if (Reflect.has(sceneObject, 'position')) {
                //@ts-ignore
                this.ad(bind([doublePointsAppendEditing, 'startPosition'], [sceneObject, 'position']));
                this.ad(bindNorthRotation([doublePointsAppendEditing, 'rotation'], [sceneObject, 'rotation']));
                do {
                    if (Reflect.has(sceneObject, 'far')) {
                        //@ts-ignore
                        this.ad(bind([doublePointsAppendEditing, 'distance'], [sceneObject, 'far']));
                        break;
                    }
                    if (Reflect.has(sceneObject, 'radius')) {
                        //@ts-ignore
                        this.ad(bind([doublePointsAppendEditing, 'distance'], [sceneObject, 'radius']));
                        break;
                    }
                    if (Reflect.has(sceneObject, 'distance')) {
                        //@ts-ignore
                        this.ad(bind([doublePointsAppendEditing, 'distance'], [sceneObject, 'distance']));
                        break;
                    }
                    // @ts-ignore
                    const centerCartesian = positionToCartesian(sceneObject.position);
                    if (Cesium.Cartesian3.ZERO.equals(centerCartesian) || !czmViewer.viewer) return;
                    doublePointsAppendEditing.distance = getSceneScaleForScreenPixelSize(czmViewer.viewer.scene, centerCartesian, 100) ?? 1;
                } while (false);
                break;
            }
            if (Reflect.has(sceneObject, 'points')) {
                //@ts-ignore
                const update = () => doublePointsAppendEditing.positions = sceneObject.points ?? [];
                update();
                //@ts-ignore
                this.ad(sceneObject.pointsChanged.don(() => !doublePointsAppendEditing.enabled && update()));
                //@ts-ignore
                this.ad(track([sceneObject, 'points'], [doublePointsAppendEditing, 'positions']));
                break;
            }
            this.supportEditing = false;
        } while (false);
        if (doublePointsAppendEditing.startPosition && !equalsN3(doublePointsAppendEditing.startPosition, [0, 0, 0])) {
            doublePointsAppendEditing.init();
        }
        doublePointsAppendEditing.enabled = true;
    }
}